import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Iterator;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author 212010509
 */
public class GeoDrawable {

    ArrayList<Handle> m_handles;
    boolean m_selected;
    boolean m_dragging;
    Handle m_draggingHandle;

    public GeoDrawable() {
        m_selected = false;
        m_dragging = false;
        m_draggingHandle = null;
        m_handles = new ArrayList<Handle>();
    }

    /**
     * draw will just draw the handles of the figure.
     *
     * @param g
     */
    public void draw(Graphics g) {
        Color oldColor = null;
        if (m_selected) {

            oldColor = g.getColor();
            g.setColor(Color.green);
            }
        Iterator<Handle> hi = m_handles.iterator();
        while (hi.hasNext()) {
            Handle h = hi.next();
            h.draw(g);
        }
        if (oldColor != null) {
            g.setColor(oldColor);
        }
    }

    public boolean selected() {
        return m_selected;
    }

    public void setSelected(boolean selected) {
        m_selected = selected;
    }

    public boolean pressed(int[] point) {
        Handle h = handleContains(point);
        if (h != null) {
            m_dragging = true;
            m_draggingHandle = h;
        }
        return h != null;
    }

    public void released(int[] point) {
        m_dragging = false;

    }

    public void drag(int[] point) {
        if (m_dragging) {
            m_draggingHandle.updateCoord(point);
        }
    }

    public Handle handleContains(int[] point) {
        Iterator<Handle> hi = m_handles.iterator();
        Handle foundHandle = null;
        while (hi.hasNext() && (foundHandle == null)) {
            Handle h = hi.next();
            if (h.contains(point)) {
                foundHandle = h;
            }
        }
        return foundHandle;
    }
}
